/*
 * Copyright (C) 2011, EADS France
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

package xowl.gmi.view;

import xowl.gmi.environment.Locale;
import xowl.gmi.environment.LongJob;
import xowl.gmi.environment.LongJobHandler;
import xowl.gmi.environment.Main;

/**
 *
 * @author Laurent WOUTERS
 */
public class WaitingWindow {
    private LongJob job;
    private javax.swing.JDialog dialog;
    private javax.swing.JLabel labelAdvancement;
    private LongJobHandler handler;
    private Object result;

    public WaitingWindow(Locale locale, LongJob job) {
        this.job = job;
        javax.swing.JFrame mainFrame = Main.getApplication().getMainFrame();
        this.dialog = new javax.swing.JDialog(mainFrame, locale.waitingTitle, true);
        this.dialog.getContentPane().setLayout(new java.awt.BorderLayout());
        this.dialog.setDefaultCloseOperation(javax.swing.JDialog.DO_NOTHING_ON_CLOSE);
        this.dialog.setSize(350, 75);
        java.awt.Dimension screen = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        this.dialog.setLocation((screen.width - dialog.getWidth()) / 2, (screen.height - dialog.getHeight()) / 2);

        this.labelAdvancement = new javax.swing.JLabel();
        this.labelAdvancement.setAlignmentX(javax.swing.JComponent.LEFT_ALIGNMENT);
        javax.swing.JLabel label_message = new javax.swing.JLabel(job.getMessage());
        label_message.setAlignmentX(javax.swing.JComponent.LEFT_ALIGNMENT);
        javax.swing.Box box_text = javax.swing.Box.createVerticalBox();
        box_text.setAlignmentY(javax.swing.JComponent.CENTER_ALIGNMENT);
        box_text.add(label_message);
        box_text.add(javax.swing.Box.createRigidArea(new java.awt.Dimension(0, 5)));
        box_text.add(labelAdvancement);
        if (job.canCancel()) {
            javax.swing.JButton buttonCancel = new javax.swing.JButton(locale.cancel_text);
            buttonCancel.setAlignmentX(javax.swing.JComponent.LEFT_ALIGNMENT);
            buttonCancel.addActionListener(new javax.swing.AbstractAction() {
                @Override public void actionPerformed(java.awt.event.ActionEvent e) { WaitingWindow.this.job.requestCancel(); }
            });
            box_text.add(buttonCancel);
        }

        javax.swing.JLabel label_animation = new javax.swing.JLabel(IconProvider.getEmbedded("waiting.gif"));
        label_animation.setAlignmentY(javax.swing.JComponent.CENTER_ALIGNMENT);
        javax.swing.Box content = javax.swing.Box.createHorizontalBox();
        content.add(label_animation);
        content.add(javax.swing.Box.createRigidArea(new java.awt.Dimension(20, 0)));
        content.add(box_text);
        this.dialog.getContentPane().add(content, java.awt.BorderLayout.CENTER);
        this.handler = new LongJobHandler() {
            @Override public void output(final String message) {
                javax.swing.SwingUtilities.invokeLater(new java.lang.Runnable() {
                    @Override public void run() { labelAdvancement.setText(message); }
                });
            }
            @Override public void setAdvance(float value) { }
        };
    }

    public Object show() {
        java.lang.Runnable runnable = new java.lang.Runnable() {
            @Override public void run() {
                result = job.run(handler);
                dialog.setVisible(false);
                dialog.dispose();
            }
        };
        java.lang.Thread thread = new java.lang.Thread(runnable, "XOWLGMIEnvironment Loader");
        thread.start();
        dialog.setVisible(true);
        return result;
    }
}
